﻿using Sustainalytics.Entities;
using System;
using Ent = Sustainalytics.Entities;
using System.Text.RegularExpressions;
using System.ComponentModel.DataAnnotations;

namespace Sustainalytics.News.Service
{
    public class NotificationsDto
    {
        public Guid Id { get; set; }

        [Required]
        public string Body { get; set; }

        [Required]
        public NotificationsStatus Status { get; set; }

        public NotificationsDto LoadFrom(Ent.Notifications data, bool truncateBody = false)
        {
            this.Id = data.Id;
            this.Body = truncateBody ? GetBodyForNews(data.Body) : data.Body;
            this.Status = data.Status;

            return this;
        }

        public string GetBodyForNews(string body)
        {
            Match m = Regex.Match(body, "^.*?<p>(.*?)</p>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            if (m.ToString().Contains("<br/>") || m.ToString().Contains("\n"))
            {
                Match m2 = Regex.Match(m.ToString(), @"(.+?(?:<br\s*\/?>|\n)+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                return m2.ToString();
            }
            else
                return m.ToString();
        }

        public void SaveTo(Ent.Notifications data)
        {
            data.Id = this.Id;
            data.Body = this.Body;
            data.Status = this.Status;
        }
    }
}
